var app = getApp()
var gb = app.globalData
var Bmob = gb.Bmob
var SessionMgr = gb.SessionMgr
var BmobObj = gb.BmobObj

var _wareId = 0
var _orderId = 0
var _consumerId = 0

Page({
  data:{
    consumer:{}
  },
  onLoad:function(options){

    _wareId = options.wareId
    _orderId = options.orderId
    _consumerId = options.consumerId
    
    this.reqWare(options.wareId)

    this.reqOrderImages(options.orderId)

    var consumerId = options.consumerId
    if(consumerId){
        this.reqConsumerInfo(consumerId)
    }

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  reqConsumerInfo:function(consumerId){
        var self = this

        var C = Bmob.Object.extend("consumer")
        var q = new Bmob.Query(C)
        q.get(consumerId,{
            success:function(result){
                var data = {}
                data.avatar = result.get("avatar")
                data.nickname = result.get("nickname")

                self.setData({
                    consumer:data
                })

                console.log(data)
            }
        })
    },
    reqWare:function(wareId){
        var self = this

        var Ware = Bmob.Object.extend("ware")
        var query = new Bmob.Query(Ware)
        query.get(wareId,function(result){

            var data = BmobObj.func.wareList([result])[0]

            var imgs = data.images.split("[/]")
            var resultsImage = []
            for(var index = 0 ; index < imgs.length ; index++){
                var item = imgs[index]

                if(index == 0 ){
                    data.ware_main_img = item
                }else{
                    resultsImage.push(item)
                }
            }
            data.ware_title = data.title
            data.ware_title_subject = data.title_subject

            self.setData({
                images:resultsImage
            })

            self.setData({
                ware:data
            })
        })
    },
    reqOrderImages:function(orderId){
        //获取成片
        var self = this

        var OS = Bmob.Object.extend("order_status")
        var q = new Bmob.Query(OS)
        q.equalTo("order_object_id",orderId)
        q.equalTo("status_code",7)
        q.find({
            success:function(results){
                var data = BmobObj.func.orderStatusList(results)[0]

                var imgs = data.images.split("[/]")
                var resultsImage = []
                for(var index = 0 ; index < imgs.length ; index++){
                    var item = imgs[index]

                    if(index == 0 ){
                        data.ware_main_img = item
                    }else{
                        resultsImage.push(item)
                    }
                }

                self.setData({
                  images:resultsImage
                })

            }
        })
    },
    onShareAppMessage: function () {
      return {
        title: SessionMgr.getConstant().appname,
        path: 'orderCaseDetail?orderId='+_orderId+'&consumerId='+_consumerId+'&wareId='+_wareId
      }
    }
})